import requests
from loguru import logger

from base.base_login import BaseLogin
from base.env_config import ENV


def hook_response(response, *args, **kwargs):
    logger.info(f"接口响应数据为：{response.json()}")

class BaseRequests(object):
    def __init__(self):
        self._session = requests.session()

        self._session.hooks['response'].append(hook_response)

class MerRequests(BaseRequests, BaseLogin):
    def __init__(self):
        super().__init__()
        self._session.headers.update({
            'authori-zation': self._merchant_login(),
            'content-type': 'application/json',
        })
    def _get(self, api_name, url: str, params: dict = None):
        logger.info(f"接口{api_name} 接口请求数据为：{params}")
        return self._session.get(ENV.MER_POST + url, params=params).json()



    def _post(self, api_name, url: str, data: dict = None):
        logger.info(f"接口{api_name} 接口请求数据为：{data}")
        return self._session.post(ENV.MER_POST + url, json=data).json()

class User(BaseRequests, BaseLogin):
    def __init__(self):
        super().__init__()
        self._session.headers.update({'authori-zation': self._user_login(), 'content-type': 'application/json'}),

    def _post(self, api_name, url: str, data: dict = None, params: dict = None):
        logger.info(f"接口{api_name} 接口请求数据为：{ENV.USER_HOST + url}, {data}, {params}")
        return self._session.post(ENV.MER_POST + url, json=data, params=params).json()

    def _get(self, api_name, url: str, data: dict = None, params: dict = None):
        logger.info(f"接口{api_name} 接口请求数据为：{ENV.USER_HOST + url}, {data}, {params}")
        return self._session.get(ENV.USER_POST + url, json=data, params=params).json()

